'use strict';
app.controller('questionCtrl',['$scope','utilService','questionService','toaster','$modal', function($scope,utilService,questionService,toaster,$modal){
    $scope.question = {
        questionTypeId:''
    };
    // call server.
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'question',
            params:{questionTypeId:$scope.question.questionTypeId}
        }).then(function(result){
        });
    }
    $scope.query();

    //删除问题
    $scope.openDelete = function (id,question) {
        var modalInstance = $modal.open({
            templateUrl: 'tpl/question/question_delete.html',
            controller: 'delQuestionCtrl',
            scope: $scope,
            size:'sm',
            resolve: {
                data: function () {
                    var data = {};
                    data.id = id;
                    data.question=question;
                    return data;
                }
            }
        });
    }

    //置顶
    $scope.toTop= function(id){
        $scope.question.id=id;
        questionService.toTop($scope).then(function(result){
            switch (result.status){
                case 200:
                    toaster.pop('success', "提示", "操作成功");
                    $scope.query();
                    break;
                case 683:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    }
}]);

//问题详情Controller
app.controller('questionDetailCtrl',['$scope','$state','$stateParams','questionService','toaster', function($scope,$state,$stateParams,questionService,toaster){
    $scope.question = {
        id:$stateParams.id
    };
    //根据id查询广告
    questionService.findById($scope).then(function(result){
        switch (result.status){
            case 200:
                $scope.question = result.data;
                break;
            case 683:
                toaster.pop('waiting', "提示", result.message);
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//查询问题类型下拉框controller
app.controller('questionTypeSelectCtrl',['$scope','questionService','toaster', function($scope,questionService,toaster){
    questionService.getList($scope).then(function (result) {
        switch (result.status) {
            case 200:
                $scope.selectList = result.data;
                break;
            default :
                toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                break
        }
    });
}]);

//删除模态框Controller
app.controller('delQuestionCtrl', ['$scope', '$modalInstance','$state','questionService','toaster','data',
    function($scope, $modalInstance,$state,questionService,toaster,data) {
        $scope.question=data;

        $scope.AllowToDelete=true;

        //确认删除
        $scope.ok=function(){
            if(!$scope.AllowToDelete){
                return;
            }
            $scope.AllowToDelete=false;
            questionService.delQuestion($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $modalInstance.dismiss('success');
                        toaster.pop('success', "提示", "操作成功");;
                        $scope.AllowToDelete=true;
                        $scope.query();
                        break;
                    case 683:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToDelete=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToDelete=true;
                        break;
                }
            });
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
//问题添加controller
app.controller('questionAddCtrl',['$scope','$state','questionService','$stateParams','toaster',
    function($scope,$state,questionService,$stateParams,toaster) {
        questionService.getList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.selectList = result.data;
                    break;
                case 683:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        //新增问题
        $scope.AllowToAdd=true;
        $scope.addQuestion = function(){
            if(!$scope.AllowToAdd){
                return;
            }
            $scope.AllowToAdd=false;
            questionService.addQuestion($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToAdd=true;
                        $state.go('app.question');
                        break;
                    default:
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToAdd=true;
                        break
                }
            });
        }
    }
]);

//问题修改controller
app.controller('questionEditCtrl',['$scope','questionService','$stateParams','toaster','$state',
    function($scope,questionService,$stateParams,toaster,$state) {
        $scope.question = {
            id:$stateParams.id
        };
        questionService.getList($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.selectList = result.data;
                    break;
                case 683:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        //根据id查询问题
        questionService.findById($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.question = result.data;
                    break;
                case 683:
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
        //修改问题
        $scope.AllowToEdit=true;
        $scope.editQuestion = function() {
            if(!$scope.AllowToEdit){
                return;
            }
            $scope.AllowToEdit=false;
            questionService.editQuestion($scope).then(function (result) {
                switch (result.status) {
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $scope.AllowToEdit=true;
                        $state.go('app.question');
                        break;
                    case 683:
                        toaster.pop('waiting', "提示", result.message);
                        $scope.AllowToEdit=true;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        $scope.AllowToEdit=true;
                        break
                }
            });
        }
    }
]);





